//
//  JGCodingHelper.m
//  JGRider
//
//  Created by 单启志 on 2018/3/7.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGCodingHelper.h"

@implementation JGCodingHelper

+(instancetype)share{
    static JGCodingHelper *helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper=[[JGCodingHelper alloc]init];
    });
    return helper;
}

-(void)loginsuccessDataEncodeWith:(id)model{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [path stringByAppendingPathComponent:LoginSuccessDataPath];
    // 归档
   BOOL success = [NSKeyedArchiver archiveRootObject:model toFile:filePath];
    if(success){
        NSLog(@"登录成功返回数据-归档成功成功");
    }
}
//读
-(JGLoginResponseModel*)loginsuccessDataDencode{

    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [path stringByAppendingPathComponent:LoginSuccessDataPath];

    //创建解归档对象，对data中的数据进行解归档
   JGLoginResponseModel *model=(JGLoginResponseModel*) [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

    return model;
}

//写
-(void)writeCityCodeToPlistWithData:(id)data{

    //把数据保存到沙盒里的plist文件
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *plistPath1= [paths objectAtIndex:0];

    NSLog(@"%@",plistPath1);
    //得到完整的路径名
    NSString *fileName = [plistPath1 stringByAppendingPathComponent:@"cityCode.plist"];
    //NSMutableDictionary *myDic = [[NSMutableDictionary alloc]init];
    //[myDic setValuesForKeysWithDictionary:cityDic];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm createFileAtPath:fileName contents:nil attributes:nil] ==YES) {

        [data writeToFile:fileName atomically:YES];
        NSLog(@"文件写入完成");
    }

}
@end
